# 'run' target assumes CG_OUTPUT_FILE is defined by command line

# these are the regular paths and definitions
INCLUDE_DIR := ../include
SRC_DIR := ../src/c
TGT_DIR := ./tgt
EXECUTABLE := $(TGT_DIR)/schemeprog
#GENERATED_CODE := code
MODULES := env machine scheme schemeobject toplevel
OBJECTS := $(addprefix $(TGT_DIR)/,$(addsuffix .o, $(MODULES) $(GENERATED_CODE) main))
SOURCES := $(addprefix $(SRC_DIR)/,$(addsuffix .c, $(MODULES) $(GENERATED_CODE) main))
HEADERS := $(addprefix $(SRC_DIR)/,$(addsuffix .h, $(MODULES)))
INCLUDES := $(INCLUDE_DIR)/*.h

CC = gcc -c -g -I$(INCLUDE_DIR)
LD = gcc

all: build

prepare:
	mkdir -p $(TGT_DIR)

clean:
	rm $(EXECUTABLE)
	rm $(OBJECTS)

build: prepare $(EXECUTABLE)

run: build
	$(EXECUTABLE) > $(CG_OUTPUT_FILE)

$(TGT_DIR)/%.o: $(SRC_DIR)/%.c $(INCLUDES) $(HEADERS)
	$(CC) -o $@ $(SRC_DIR)/$*.c

$(EXECUTABLE): $(OBJECTS)
	$(LD) -o $@ $^

.PHONY: clean prepare build run

.IGNORE: clean prepare

